package com.lanou.hotel.controller;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.lanou.hotel.entity.Home;
import com.lanou.hotel.entity.Image;
import com.lanou.hotel.service.IHomeService;
import com.lanou.hotel.service.IImageService;
import com.lanou.hotel.service.IUserService;
import com.lanou.hotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author msy
 * @since 2023-10-28
 */
@RestController
public class HomeController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IHomeService homeService;
    @Autowired
    private IImageService iImageService;
    @Value("${server.port}")
    private int port;

    /**
     * 分页查询房间列表
     * @param pageNum
     * @param pageSize
     * @param typeName
     * @return
     */
    @PostMapping("/home/getHomes")
    public ResultVO<Map<String,Object>> getHomes(Integer pageNum, Integer pageSize, String typeName){
        return homeService.getHome(pageNum,pageSize,typeName);
    }

    /**
     * 获取所有房间
     * @return
     */
    @GetMapping("/home/getAllHomes")
    public ResultVO<List<Home>> getAllHomes(){
        List<Home> homeList = homeService.list();
        if (homeList != null) {
            return ResultVO.success("请求成功",homeList);
        }else{
            return ResultVO.failed("系统繁忙，请稍后重试...");
        }
    }

    /**
     * 添加房间
     * @param home
     * @return
     */
    @PostMapping("/home/addHome")
    public ResultVO<Map<String,Object>> addHomes(@RequestBody Home home){
        try {
            homeService.saveHome(home);
            return ResultVO.success("添加成功",null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取单个房间 以及房间关联的所有图片
     * @param id
     * @return
     */
    @PostMapping("/home/getSingleHome")
    public ResultVO<Home> getSingleHome(@RequestParam("id") String id){
        //获取房间
        Home home = homeService.getById(id);
        if (home != null) {
            return ResultVO.success("获取成功",home);
        }
        return ResultVO.failed("获取失败");
    }

    /**
     * 保存已修改的房间
     * @param home
     * @return
     */
    @PostMapping("/home/saveEditedHome")
    public ResultVO<Home> saveEditedHome(@RequestBody Home home){
        try {
            homeService.updateHome(home);
            return ResultVO.success("修改成功",null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据id删除房间
     * @param homeId
     * @return
     */
    @PostMapping("/home/deleteHome")
    public ResultVO deleteHome(@RequestParam("homeId") String homeId){
        try{
            homeService.removeByIdAndRemoveImages(homeId);
            return ResultVO.success("删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVO.failed("删除失败");
        }
    }
}
